package com.j2eefast.bussiness.contract.service;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j2eefast.bussiness.contract.entity.BusRepairEntity;
import com.j2eefast.bussiness.contract.entity.HrPersonnelEntity;
import com.j2eefast.bussiness.contract.entity.HrTrainEntity;
import com.j2eefast.bussiness.contract.mapper.BusEquipmentMapper;
import com.j2eefast.bussiness.contract.mapper.HrPersonnelMapper;
import com.j2eefast.bussiness.contract.mapper.HrTrainMapper;
import com.j2eefast.common.core.page.Query;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.framework.annotation.DataFilter;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * <p>培训管理</p>
 *
 * @author: summer
 * @date: 2021-05-31 14:41
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */

@Service
public class HrTrainService extends ServiceImpl<HrTrainMapper, HrTrainEntity> {

    @Resource
    protected HrTrainMapper hrTrainMapper;

    @Resource
    protected BusEquipmentMapper busEquipmentMapper;

//    /**
//     * 页面分页查询
//     */
//    public PageUtil findPage(Map<String, Object> params) {
//        QueryWrapper<HrTrainEntity> r = new QueryWrapper<HrTrainEntity>();
//        Page<HrTrainEntity> page = this.baseMapper.selectPage(new Query<HrTrainEntity>(params).getPage(), r);
//        return new PageUtil(page);
//    }

    /**
     * 页面分页查询
     */
    public PageUtil findPage(Map<String, Object> params) {
        String userCode = (String) params.get("userCode");
        String userCode2 = (String) params.get("userCode2");
        String userName = (String) params.get("userName");
        String userName2 = (String) params.get("userName2");
        String sex = (String) params.get("sex");
        String depId = (String) params.get("depId");
        String position = (String) params.get("position");
        String remark = (String) params.get("remark");
        String beginTime = (String) params.get("beginTime");
        String endTime = (String) params.get("endTime");
        Page<HrTrainEntity> page = hrTrainMapper.findPage(
                new Query<HrTrainEntity>(params).getPage(),
                StrUtil.nullToDefault(userCode, ""),
                StrUtil.nullToDefault(userCode2, ""),
                StrUtil.nullToDefault(userName, ""),
                StrUtil.nullToDefault(userName2, ""),
                StrUtil.nullToDefault(sex, ""),
                StrUtil.nullToDefault(depId, ""),
                StrUtil.nullToDefault(position, ""),
                StrUtil.nullToDefault(remark, ""),
                StrUtil.nullToDefault(beginTime, ""),
                StrUtil.nullToDefault(endTime, "")
        );
        return new PageUtil(page);
    }

    public boolean updateQuit(HrTrainEntity hrTrainEntity){
        return hrTrainMapper.updateQuit(hrTrainEntity);
       // return true;
    }

//    public boolean repairSave(HttpServletRequest request, Model model) throws ParseException {
//        String total_items = request.getParameter("total_items");//认领插入条数
//        int equipmentId =Integer.parseInt(request.getParameter("equipmentId"));
//        //int ids = Integer.parseInt(request.getParameter("id"));//认领插入条数
//        ValidatorUtil.validateEntity(total_items);
//        int total = Integer.valueOf(total_items);
//        List<BusRepairEntity> recordList = new ArrayList<BusRepairEntity>();//更新数据放这里面
//        List<BusRepairEntity> recordList1 = new ArrayList<BusRepairEntity>();//新增数据放这里
//        BusRepairEntity user = new BusRepairEntity();
//        BusEquipmentEntity busEquipmentEntity = busEquipmentMapper.equipmentInfo(equipmentId);
//
//        for (int i = 1; i <= total; i++) {
//            String id = "id_" + i;
//            String sendTime = "sendTime_" + i;
//            String repairTime = "repairTime_" + i;
//            String repairMoney = "repairMoney_" + i;
//            String repairStatus = "repairStatus_" + i;
//            String faultReason = "faultReason_" + i;
//            String remark = "remark_" + i;
//            Date date = new Date();//创建一个时间对象，获取到当前的时间
//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
//            user.setSendTime(simpleDateFormat.parse(request.getParameter(sendTime)));
//            user.setRepairTime(simpleDateFormat.parse(request.getParameter(repairTime)));
//            user.setRepairMoney(request.getParameter(repairMoney));
//            user.setRepairStatus(request.getParameter(repairStatus));
//            user.setFaultReason(request.getParameter(faultReason));
//            user.setRemark(request.getParameter(remark));
//            user.setEquipmentId(equipmentId);
//            user.setEquipmentCode(busEquipmentEntity.getEquipmentCode());
//            user.setEquipmentName(busEquipmentEntity.getEquipmentName());
//            user.setEquipmentModel(busEquipmentEntity.getEquipmentModel());
//            user.setMeteringUnit(busEquipmentEntity.getMeteringUnit());
//            if (request.getParameter(id) != "") {
//                user.setId(Integer.parseInt(request.getParameter(id)));
//                //先去查询是否有记录
//                BusRepairEntity info = this.repairInfo(user.getId());
//                if (info != null) {
//                    recordList.add(user);
//                } else
//                    recordList1.add(user);
//            }else
//                recordList1.add(user);
//        }
//
//        if (recordList != null && !recordList.isEmpty()) {
//            this.updateRepair(recordList);
//        }
//        if (recordList1 != null && !recordList1.isEmpty()) {
//            return this.saveRepair(recordList1) ? true : false;
//        }
//        return true;
//    }

    @Transactional(rollbackFor = Exception.class)
    boolean saveRepair( List<HrTrainEntity> recordList){
        return hrTrainMapper.saveRepair(recordList);
    }

    @Transactional(rollbackFor = Exception.class)
   boolean updateRepair( List<HrTrainEntity> recordList){
        return hrTrainMapper.updateRepair(recordList);
   }



   public HrTrainEntity repairInfo(int id){
        return hrTrainMapper.repairInfo(id);
    }


   public List<HrTrainEntity> repairList(int id){
        return hrTrainMapper.repairList(id);
    }

    public boolean deleteByIds(Long[] ids) {

        return hrTrainMapper.deleteByIds(ids);

    }

    public boolean updateCheck(Long id) {
        Long userId= UserUtils.getUserInfo().getId();

        return hrTrainMapper.updateCheck(id ,userId);

    }


//    @DataFilter(compAlias="c",deptAlias = "d",userAlias = "u")
    public List<HrTrainEntity> findList(Map<String, Object> params) {
        String userCode = (String) params.get("userCode");
        String userCode2 = (String) params.get("userCode2");
        String userName = (String) params.get("userName");
        String userName2 = (String) params.get("userName2");
        String sex = (String) params.get("sex");
        String depId = (String) params.get("depId");
        String position = (String) params.get("position");
        String remark = (String) params.get("remark");
        String beginTime = (String) params.get("beginTime");
        String endTime = (String) params.get("endTime");
//        Long[] ids  = (Long[]) params.get("ids");
        List<HrTrainEntity> page = hrTrainMapper.findList(
//                new Query<HrTrainEntity>(params).getPage(),
                StrUtil.nullToDefault(userCode, ""),
                StrUtil.nullToDefault(userCode2, ""),
                StrUtil.nullToDefault(userName, ""),
                StrUtil.nullToDefault(userName2, ""),
                StrUtil.nullToDefault(sex, ""),
                StrUtil.nullToDefault(depId, ""),
                StrUtil.nullToDefault(position, ""),
                StrUtil.nullToDefault(remark, ""),
                StrUtil.nullToDefault(beginTime, ""),
                StrUtil.nullToDefault(endTime, ""));
        return page;
    }






}
